#include "sbi.h"
#include "stdio.h"
#include "types.h"

void putchar(char ch) {
	console_putchar(ch);
}

char getchar(void) {
	return console_getchar();
}

void puts(char *chars) {
	char *p = NULL;
	for (p = chars; *p != '\0'; ++p) {
		putchar(*p);
	}
}

void print_uint(uint64 num) {
	char chars[64];
	int index = 0;
	if (num == 0) {
		putchar('0');
		return;
	}
	while (num != 0) {
		chars[index] = (num % 10) + '0';
		++index;
		num /= 10;
	}
	--index;
	while(index >= 0) {
		putchar(chars[index]);
		--index;
	}
	return;
}

void debug_uint(char *str, uint64 num) {
	putchar('\n');
	puts(str);
	putchar(':');
	print_uint(num);
	putchar('\n');
}

void byte2hex(char byte) {
	char trans[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
	int array[3] = {0}, i = 0;
	if (byte == 0) {
		puts("00");
		return;
	}
	while (byte != 0 && i <= 3) {
		array[i] = byte % 16;
		++i;
		byte /= 16;
	}
	--i;
	
	while (i >= 0) {
		putchar(trans[array[i]]);
		--i;
	}
}

void print_cluster(char *p) {
	int i;
	putchar('\n');
	for (i = 1; i <= 4096; ++i) {
		byte2hex(p[i - 1]);
		putchar(' ');
		if (i % 16 == 0) {
			putchar('\n');
		}
	}
}